﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KZ.Express.Setup.KZSystem
{
    public partial class PermissionView : FormBase
    {
        string _permissionID;
        Dictionary<int, CheckBox> checkboxs = new Dictionary<int, CheckBox>();

        public PermissionView()
        {
            InitializeComponent();
            this.TopLevel = true;
            _presenter = new PresenterBase(this, new PermissionENT());
        }

        private void PermissionView_Load(object sender, EventArgs e)
        {
            SearchCriteria search = new SearchCriteria();
            search.Add("PermissionID", this.ObjectType);

            DataTable permissions = _presenter.LoadDataTable<PermissionENT>(search);
            foreach (DataRow row in permissions.Rows)
            {
                CheckBox chk = new CheckBox();
                chk.Text = Utility.StringParse(row["Description"]);
                chk.Visible = true;

                int id = Utility.IntParse(row["PermissionID"]);
                if (_permissionID.Contains("," + id + ","))
                    chk.Checked = true;

                checkboxs.Add(id, chk);

                flowLayoutPanel1.Controls.Add(chk);
            }
        }

        public string ObjectType { get; set; }

        public string PermissionID
        {
            get
            {
                string permissionID = String.Empty;
                foreach (KeyValuePair<int, CheckBox> chk in checkboxs)
                {
                    if (chk.Value.Checked)
                    {
                        permissionID += chk.Key + ",";
                    }
                }

                if (permissionID.Length > 0) permissionID = permissionID.Remove(permissionID.Length - 1);

                return permissionID;
            }
            set { _permissionID = "," + value + ","; }
        }

        public string Permission
        {
            get
            {
                string permission = String.Empty;
                foreach (KeyValuePair<int, CheckBox> chk in checkboxs)
                {
                    if (chk.Value.Checked)
                    {
                        permission += chk.Value.Text + ", ";
                    }
                }

                if (permission.Length > 0) permission = permission.Remove(permission.Length - 2);

                return permission;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Yes;
        }
    }
}
